package com.qdxwx.web.controller.teacher;

import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.data.service.book.BookService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.dict.DictDataService;
import com.qdxwx.data.service.subject.SubjectService;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.subject.Subject;
import com.qdxwx.models.teacher.Teacher;
import com.qdxwx.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;

/**
 * Teacher接口
 * @author developer
 */
@Controller
@RequestMapping("/teacher")
public class TeacherViewController extends BaseController {

	@Autowired
	private TeacherService teacherService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private SubjectService subjectService;
	@Autowired
	private BookService bookService;
	@Autowired
	private DictDataService dictDataService;

	/**
	 * 教师详情
	 */
	@GetMapping("/view/{teacherId:[0-9]+}.html")
	public String teacherInfo(HttpServletRequest request, @PathVariable int teacherId, ModelMap model) {
		// 查询老师
		Teacher teacher = teacherService.get(teacherId);
		if (teacher == null) {
			return VIEW_ERROR_404;
		}
		model.put("teacher", teacher);
		CourseQuery courseQuery = new CourseQuery();
		courseQuery.setChannelId(2);
		courseQuery.setIsAvailable(1);
		courseQuery.setTeacherId(teacherId);
		long courseCount = courseService.count(courseQuery);
		model.put("courseCount", courseCount);
		courseQuery.setChannelId(8);
		long proCount = courseService.count(courseQuery);
		model.put("proCount", proCount);
		int bookCount = bookService.countByTeacherId(teacher.getId());
		model.put("bookCount", bookCount);
		String starName = dictDataService.getDictLabel("qdx_teacher_title", teacher.getStarId().toString());
		String subjectName = "-";
		if (teacher.getSubjectId() > 0) {
			Subject subject = subjectService.get(teacher.getSubjectId());
			if (subject != null) {
				subjectName = subject.getSubjectName();
			}
		}
		model.put("subjectName", subjectName);
		model.put("starName", starName);

		teacherService.updateViewCount(teacherId);

		if (TKRequest.isMobileBrowse(request)) {
			return getViewPathH5("teacher/teacher-view");
		}
		return getViewPath("teacher/teacher-view");
	}
}